// pages/medicalRecord/medicalRecord.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    patientTel: '',
    patientTime: '',
    patientOrderDate: '',
    illName: '',
    illLevel: '',
    iLindex: 0,
    iLArray: ["1","2","3","4","5"],
    illResult: '',
    illCare: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({   
      username: getApp().globalData.username,
      patientTel: JSON.parse(options.patientTel),
      patientTime: JSON.parse(options.patientOrderTime),
      patientOrderDate: JSON.parse(options.patientOrderDate) 
    })
  },

  /**
   * 医生选择疾病等级
   */
  illLevelBindPickerChange: function(e){
    this.setData({
      iLindex: e.detail.value,
    })
    this.setData({
      illLevel: this.data.iLArray[this.data.iLindex],
    })
  },

  /** 
   * 医生输入病情名称
  */
  nameInput: function(e){
    this.setData({
      illName: e.detail.value
    })
  },

  /** 
   * 医生输入检查结果
  */
  resultInput: function(e){
    this.setData({
      illResult: e.detail.value
    })
  },

  /** 
   * 医生输入检查结果
  */
  careInput: function(e){
    this.setData({
      illCare: e.detail.value
    })
  },

  /**
   * 医生开好病历
   */
  submitBtnClick: function(e){
    if (this.data.illName == "" || this.data.illName == undefined) 
    {
      wx.showToast({
        title: '请输入病名',
        icon: 'none'
      })
      return;
    } 
    if (this.data.illLevel == "" || this.data.illLevel == undefined) 
    {
      wx.showToast({
        title: '请选择疾病等级',
        icon: 'none'
      })
      return;
    } 
    if (this.data.illResult == "" || this.data.illResult == undefined) 
    {
      wx.showToast({
        title: '请输入检查结果',
        icon: 'none'
      })
      return;
    } 
    else{
      console.log(this.data.username)
      console.log(this.data.username)
      wx.request({
        url: getApp().globalData.url + 'illRecordServlet',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          doctorTel: this.data.username,
          patientTel: this.data.patientTel,
          illName: this.data.illName,
          illLevel: this.data.illLevel,
          illResult: this.data.illResult,
          illCare: this.data.illCare,
          orderDate: this.data.patientOrderDate,
          orderTime: this.data.patientTime
        },
        success(res) {
          wx.showToast({
            title: '成功开好病历',
            icon: 'success',
          })
          wx.redirectTo({
            url: '../todayAppointments/todayAppointments',
          })
        },
        fail(res) {
          wx.showToast({
            title: '保存失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  }
})